package com.share.config.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AliPay {
    @Autowired
    private AliPayProperties properties;

    public String pay(String tradeNo, String totalAmount, String subject, String body) throws AlipayApiException {
        DefaultAlipayClient client = new DefaultAlipayClient(properties.getGetewayUrl(), properties.getAppid(), properties.getPrivateKey(), "json", properties.getCharset(), properties.getPublicKey(), properties.getSignType());

        //商品信息
        AliPayReqParam param = new AliPayReqParam();
        param.setBody(body);
        param.setSubject(subject);
        param.setOut_trade_no(tradeNo);
        param.setTotal_amount(totalAmount);

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl(properties.getReturnUrl());
        request.setNotifyUrl(properties.getNotifyUrl());
        request.setNotifyUrl(JSON.toJSONString(param));


        //执行支付
        String result = client.pageExecute(request).getBody();

        return result;
    }
}
